using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using TnPlus.Entities.Models;
using TnPlus.Entities.Utils;
using TnPlus.Common;

namespace TnPlus.DAL.Base
{
	public class CallSMSDAOBase
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors

		public CallSMSDAOBase(string connectionStringName)
		{
			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the CallSMS table.
		/// </summary>
		public virtual void Insert(CallSMSInfo callSMSInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", callSMSInfo.IdCallSMS),
				new SqlParameter("@PostDate", callSMSInfo.PostDate),
				new SqlParameter("@IdCommon", callSMSInfo.IdCommon),
				new SqlParameter("@AccountNo", callSMSInfo.AccountNo),
				new SqlParameter("@Asset", callSMSInfo.Asset),
				new SqlParameter("@Debt", callSMSInfo.Debt),
				new SqlParameter("@Ratio", callSMSInfo.Ratio),
				new SqlParameter("@CallLMV", callSMSInfo.CallLMV),
				new SqlParameter("@ForceLMV", callSMSInfo.ForceLMV),
				new SqlParameter("@Action", callSMSInfo.Action),
				new SqlParameter("@AddCash", callSMSInfo.AddCash),
				new SqlParameter("@ContentSMS", callSMSInfo.ContentSMS),
				new SqlParameter("@ReceiveNo", callSMSInfo.ReceiveNo),
				new SqlParameter("@Status", callSMSInfo.Status)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CallSMS_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the CallSMS table.
		/// </summary>
		public virtual void Update(CallSMSInfo callSMSInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", callSMSInfo.IdCallSMS),
				new SqlParameter("@PostDate", callSMSInfo.PostDate),
				new SqlParameter("@IdCommon", callSMSInfo.IdCommon),
				new SqlParameter("@AccountNo", callSMSInfo.AccountNo),
				new SqlParameter("@Asset", callSMSInfo.Asset),
				new SqlParameter("@Debt", callSMSInfo.Debt),
				new SqlParameter("@Ratio", callSMSInfo.Ratio),
				new SqlParameter("@CallLMV", callSMSInfo.CallLMV),
				new SqlParameter("@ForceLMV", callSMSInfo.ForceLMV),
				new SqlParameter("@Action", callSMSInfo.Action),
				new SqlParameter("@AddCash", callSMSInfo.AddCash),
				new SqlParameter("@ContentSMS", callSMSInfo.ContentSMS),
				new SqlParameter("@ReceiveNo", callSMSInfo.ReceiveNo),
				new SqlParameter("@Status", callSMSInfo.Status)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CallSMS_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the CallSMS table by its primary key.
		/// </summary>
		public virtual void Delete(Guid? idCallSMS)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", idCallSMS)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "CallSMS_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the CallSMS table.
		/// </summary>
		public virtual CallSMSInfo Select(Guid? idCallSMS)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCallSMS", idCallSMS)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CallSMS_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeCallSMSInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the CallSMS table.
		/// </summary>
		public List<CallSMSInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "CallSMS_SelectAll"))
			{
				List<CallSMSInfo> callSMSInfoList = new List<CallSMSInfo>();
				while (dataReader.Read())
				{
					callSMSInfoList.Add(MakeCallSMSInfo(dataReader));
				}

				return callSMSInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the CallSMS class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual CallSMSInfo MakeCallSMSInfo(SqlDataReader dataReader)
		{
			CallSMSInfo callSMSInfo = new CallSMSInfo();
			callSMSInfo.IdCallSMS = SqlClientUtility.GetGuid(dataReader, DbConstants.CALLSMS.IDCALLSMS);
			callSMSInfo.PostDate = SqlClientUtility.GetDateTime(dataReader, DbConstants.CALLSMS.POSTDATE);
			callSMSInfo.IdCommon = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMS.IDCOMMON);
			callSMSInfo.AccountNo = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMS.ACCOUNTNO);
			callSMSInfo.Asset = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMS.ASSET);
			callSMSInfo.Debt = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMS.DEBT);
			callSMSInfo.Ratio = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMS.RATIO);
			callSMSInfo.CallLMV = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMS.CALLLMV);
			callSMSInfo.ForceLMV = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMS.FORCELMV);
			callSMSInfo.Action = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMS.ACTION);
			callSMSInfo.AddCash = SqlClientUtility.GetDecimal(dataReader, DbConstants.CALLSMS.ADDCASH);
			callSMSInfo.ContentSMS = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMS.CONTENTSMS);
			callSMSInfo.ReceiveNo = SqlClientUtility.GetString(dataReader, DbConstants.CALLSMS.RECEIVENO);
			callSMSInfo.Status = SqlClientUtility.GetBoolean(dataReader, DbConstants.CALLSMS.STATUS);

			return callSMSInfo;
		}

		#endregion
	}
}
